var app = getApp();

Page({
  data: {
    inputShowed: false,
    inputVal: "",
    searchPreview:"",
    result:"",
    keys: []
    
  },
  onLoad:function(){
    var that = this;
    wx.request({
      url: app.globalData.phpUrl + 'Publish/api',
      method: 'POST',
      data: { api_name: 'key_search', 'openid': app.globalData.userData.openOId},
      success: function (d) {
        that.setData({ keys: d.data.data})
      }
    });
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      searchPreview: ""
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: "",
      searchPreview:[]
    });
  },
  wxSearchKeyTap:function(e){
    var that = this;
    var key = e.currentTarget.dataset.key;
    wx.request({
      url: app.globalData.phpUrl + 'Publish/api',
      method: 'POST',
      data: { api_name: 'question_bank', openid: app.globalData.userData.openOId, search: key },
      success: function (d) {
        that.setData({
          result: d.data.data.list,
          showLad: false
        })
      }
    });
  },
  getResult:function(e){
    var that = this;
    that.setData({
      showLad: true,
      searchPreview: ''
    });

    wx.request({
      url: app.globalData.phpUrl + 'Publish/api',
      method: 'POST',
      data: { api_name: 'question_bank', openid: app.globalData.userData.openOId, search: e.target.dataset.content},
      success: function (d) {
        that.setData({
          result: d.data.data.list,
          showLad: false
        })
      }
    });
  },
  
  confirmTap:function(e){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: app.globalData.phpUrl + 'Publish/api',
      method: 'POST',
      data: { api_name: 'question_bank', openid: app.globalData.userData.openOId, search: e.detail.value },
      success: function (d) {
        wx.hideLoading();
        that.setData({
          result: d.data.data.list,
          showLad: false,
          searchPreview: []
        })
      }
    });
  },

  inputTyping: function (e) {
    var that = this;
    if (e.detail.value == ''){
         that.setData({
           searchPreview: []
         })
    }else{
      wx.request({
        url: app.globalData.phpUrl + 'Publish/api',
        method: 'POST',
        data: { api_name: 'key_search', 'openid': app.globalData.userData.openOId, 'search': e.detail.value },
        success: function (d) {
          that.setData({
            searchPreview: d.data.data
          })
        }
      });
    }
    
    this.setData({
      inputVal: e.detail.value
    });
  },

  choseItem:function(e){
   wx.navigateTo({
     url: '../choseItem/choseItem?id=' + e.target.dataset.id,
   })
  }

});